<!--
 * @Author: HeAo
 * @Date: 2021-10-13 11:40:11
 * @LastEditTime: 2021-10-13 12:03:02
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2105\3-2-Vue\day05\7-编程式导航.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
  <style>
    header {
      height: 50px;
      /* border: 1px solid blue; */
      /* margin: 10px; */
      background-color: cadetblue;
      color: white;
    }

    main {
      /* height: calc(100% - 150px); */
      height: 700px;
      display: flex;
    }

    .left {
      width: 150px;
      height: 100%;
      border: 3px solid green;
      margin: 10px;
    }

    .right {
      width: calc(100% - 150px);
      height: 100%;
      border: 3px solid orange;
      margin: 10px;
    }
  </style>
</head>

<body>
  <div id="app">
    <header>
      我是网站头部
    </header>
    <main>
      <div class="left">
        <!-- 路由跳转链接 -->
        <div>
          <router-link to="/user">用户管理</router-link>
        </div>
        <div>
          <router-link to="/student">学生管理</router-link>
        </div>
        <div>
          <router-link to="/clazz">班级管理</router-link>
        </div>
      </div>
      <div class="right">
        <!-- 路由对应组件加载的窗口 -->
        <router-view></router-view>
      </div>
    </main>
  </div>
  <script>
    // 1.声明组件
    let com1 = {
      data() {
        return {}
      },
      template: `
        <div>
          用户管理
        </div>
      `
    }
    let com2 = {
      data() {
        return {}
      },
      methods: {
        clickHandler(e) {
          if (e.target.innerText == '成绩管理') {
            // this.$router.push()编程式导航方法
            // this.$router.push({ path: '/student/grade' })
            // this.$router.push('/student/grade')
            // this.$router.push({ name: 'grade' })

            // 路由跳转并且携带参数
            this.$router.push({
              path: '/student/grade',
              query: {
                id: 1001,
                name: 'zhangsan'
              }
            })
            // this.$router.push({
            //   name: 'grade',
            //   params: {
            //     id: 1001,
            //     name: 'zhangsan'
            //   }
            // })
          } else {
            // this.$router.push({ path: '/student/register' })
            this.$router.push({ name: 'register' })
          }
        }
      },
      template: `
        <div>
          学生管理
          <div>
            <button @click="clickHandler">成绩管理</button>
            <button @click="clickHandler">学籍管理</button>
          </div>
          <router-view></router-view>
        </div>
      `
    }
    let com3 = {
      data() {
        return {}
      },
      template: `
        <div>
          班级管理
        </div>
      `
    }
    let comGrade = {
      data() {
        return {}
      },
      template: `
        <div>
          成绩管理
        </div>
      `,
      created() {
        console.log('query', this.$route.query);
        console.log('params', this.$route.params);
      }
    }
    let comRegister = {
      data() {
        return {}
      },
      template: `
        <div>
          学籍管理
        </div>
      `
    }
    // 2.定义路由
    let router = new VueRouter({
      // 修改路由模式
      mode: 'history',
      routes: [
        {
          path: '/user',   // 动态路由，冒号开头的字段是动态参数
          component: com1,// 路径对应加载的组件
          name: 'user'
        },
        {
          path: '/student',
          component: com2,
          children: [
            { path: 'grade', component: comGrade, name: 'grade' },
            { path: 'register', component: comRegister, name: 'register' },
          ]
        },
        { path: '/clazz', component: com3 },
      ]
    })

    // 3.注册路由、组件
    let vm = new Vue({
      el: '#app',
      data: {},
      methods: {},
      router
    })
  </script>
</body>

</html>